<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title></title>
		<link href="<%=application.getContextPath()%>/nresources/ued/css/all-src.css" type="text/css" rel="stylesheet" />
		<%@include file="/WEB-INF/ibnms/common/easyui.jsp" %>
		<%@include file="/WEB-INF/ibnms/common/jqgrid.jsp"%>
        <%@ include file="/WEB-INF/ibnms/common/formValidation.jsp"%>
        <jsp:include page="/WEB-INF/ibnms/common/configinclude.jsp"></jsp:include>
        <script src="<%=application.getContextPath()%>/njs/basd/uplodify/jquery.uploadify.min.js?f=<%=System.currentTimeMillis()%>" type="text/javascript"></script>
        <link rel="stylesheet" type="text/css" href="<%=application.getContextPath()%>/njs/basd/uplodify/uploadify.css">

        <link href="<%=application.getContextPath()%>/njs/basd/downloadr/facebox/facebox.css" media="screen" rel="stylesheet" type="text/css"/>
        <script src="<%=application.getContextPath()%>/njs/basd/downloadr/facebox/facebox.js" type="text/javascript"></script>
        <link href="<%=application.getContextPath()%>/njs/basd/downloadr/downloadr/downloadr.css" media="screen" rel="stylesheet" type="text/css"/>
        <script src="<%=application.getContextPath()%>/njs/basd/downloadr/downloadr/jqbrowser.js" type="text/javascript"></script>
        <script src="<%=application.getContextPath()%>/njs/basd/downloadr/downloadr/downloadr.js" type="text/javascript"></script>
        <%@include file="/WEB-INF/ibnms/common/jquery-tokeninput.jsp" %>
        <script type="text/javascript" src="<%=application.getContextPath()%>/njs/basd/My97DatePicker/WdatePicker.js"></script>

        <style type="text/css">
            body {
                font: 13px Arial, Helvetica, Sans-serif;
            }
            .selecButton{
                background-color: #000;
                border-color:white;
                border:0px 0px;
                border-radius:0px;
                box-shadow:0 1px 3px rgba(0,0,0,0.25);
                margin-bottom: 60px;
                padding:60px 60px;
                overflow: auto;
                padding:1px 1px;
                background: url('<%=application.getContextPath()%>/njs/basd/uplodify/select.jpg') 50% 50% no-repeat;
            }
        </style>
    </head>
<body>
<div class="form-wrapper">
    <div class="form-title">
        <span class="ttcorner"></span>
        <h3>
            <span class="text" style="text-align: center;">增加</span>
        </h3>
    </div>
    <div class="box-in">
        <form id="theForm" action="<s:url action="addNews"/>" method="post">
            <table class="table-03" width="100%" height="83" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <input type="hidden" name="news.ID" id="news.ID"  value='${news.ID}' />
                    <td width="15%" class="monitor_bg1">标题：</td>
                    <td width="85%" class="monitor_bg1">
                        <input type="text" name="news.TITLE" id="news.TITLE"
                               class="input-style1"  value='${news.TITLE}'style=" width:500px"/>
                    </td>
                </tr>

                <tr>
                    <td class="monitor_bg1">内容：</td>
                    <td class="monitor_bg1">
                        <textarea name="news.CONTENT" id="news.CONTENT" cols="80" rows="15">${news.CONTENT}</textarea>
                    </td>
                </tr>

                <tr>
                    <td class="monitor_bg1">接收部门：</td>
                    <td class="monitor_bg1">
                        <input type="hidden" name="news.RCV_DEPT_ID" id="news.RCV_DEPT_ID"  tip='RCV_DEPT'/>
                        <input type="hidden" name="news.RCV_DEPT_NAME" id="news.RCV_DEPT_NAME"  />

                    </td>
                </tr>

                <tr>
                    <td class="monitor_bg1">接收人员：</td>
                    <td class="monitor_bg1" >
                        <input type="hidden" id="news.RCV_LOGIN_ID" name="news.RCV_LOGIN_ID" tip='RCV_LOGIN' >
                        <input type="hidden" id="news.RCV_LOGIN_NAME"    name="news.RCV_LOGIN_NAME"  >
                    </td>
                </tr>

                <tr>
                    <td class="monitor_bg1">生效时间：</td>
                    <td class="monitor_bg1" >
                        <input type="text" name="news.PUBLISH_TIME" value="${news.PUBLISH_TIME}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" class="Wdate" style="width:150px"/>
                    (若为空,则立即生效)
                    </td>
                </tr>

                <tr>
                    <td class="monitor_bg1">失效时间：</td>
                    <td class="monitor_bg1" >
                        <input type="text" name="news.FAILURE_TIME" value="${news.FAILURE_TIME}" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" class="Wdate" style="width:150px"/>
                    (若为空,则永不失效)
                    </td>
                </tr>


                <tr>
                    <td class="monitor_bg1">附件：</td>
                    <td class="monitor_bg1" id='fileTd'>
                        <input type="file" id="file_upload"/>
                        <div id="errorMsg"></div>

                    </td>
                </tr>

                <tr>
                    <td colspan="2" nowrap>
                        <div align="center">
                            <input type="button" id="btn-save" class="btn-style02" value="保存"/>&nbsp;&nbsp;
                            <input type="button" class="btn-style02" value="返回"  id='btn-back' onclick="  window.close();"/>
                        </div>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>
<script type="text/javascript">

    var saveFileIds = [];
    var fileDivPathMap = {};
    var fileIdMap = {};


    var fileInfos = '${news.FILE_INFO}'==''?'':'${news.FILE_INFO}'.split(",") ;
    var fileIds = [] ;
    var fileNames =[] ;
    var filePaths = [] ;
    $.each(fileInfos||[],function(k,v){
        if(v.indexOf("@")>=0){
            var arr = v.split("@");
            fileIds.push(arr[0]);
            fileNames.push(arr[1]);
            filePaths.push(arr[2]);
        }
    })


    var rcvLoginIds = '${news.RCV_LOGIN_ID}'==''?'':'${news.RCV_LOGIN_ID}'.split(",") ;
    var rcvLoginNames = '${news.RCV_LOGIN_NAME}'==''?'':'${news.RCV_LOGIN_NAME}'.split(",") ;
    var rcvDeptIds = '${news.RCV_DEPT_ID}'==''?'':'${news.RCV_DEPT_ID}'.split(",") ;
    var rcvDeptNames = '${news.RCV_DEPT_NAME}'==''?'':'${news.RCV_DEPT_NAME}'.split(",") ;


    $.each(fileIds||[],function(k,v){
        var url = 'download.do?savePath='+ encodeURIComponent(filePaths[k])+'&fileFileName='+encodeURIComponent(fileNames[k]);
        $("#fileTd").append(""+
             "<div id="+fileIds[k]+" class='uploadify-queue-item'>"+
                "<div class='cancel'> <a href='javascript:$(\"#file_upload\").uploadify(\"cancel\", \""+fileIds[k]+"\")'>X</a></div> "+
                "<a href="+url+" " +
                "rel='downloadr' >"+fileNames[k]+"</a></span>"+
             "</div>")

    })

    $(function(){

        $.each(rcvDeptIds||[],function(k,v){
            $("[id='news.RCV_DEPT_ID']").tokenInput("add", {id: v, name:rcvDeptNames[k]});
        })

        $.each(rcvLoginIds||[],function(k,v){
            $("[id='news.RCV_LOGIN_ID']").tokenInput("add", {id: v, name:rcvLoginNames[k]});
        })

        $.fn.uploadify=$.wrap($.fn.uploadify,function(op,val){
            if(op=='cancel' && fileDivPathMap[val]){
                $.ajax({//填充告警数
                    url:'<s:url action="delFile" namespace="/"/>',
                    data:{savePath:fileDivPathMap[val]},
                    type:'post',
                    success:function(data){
                        delete fileDivPathMap[val];

                        $.each(saveFileIds||[],function(k,v){
                            if(v == fileIdMap[val]){
                                saveFileIds.splice(k,1);
                                return false;
                            }
                        })

                        $.each(fileIds||[],function(k,v){
                            if(v == fileIdMap[val]){
                                fileIds.splice(k,1);
                                return false;
                            }
                        })

                    }
                });
            }else{
                $.each(fileIds||[],function(k,v){
                    if(v == val){
                        fileIds.splice(k,1);
                        return false;
                    }
                })
            }



        });

        $('#file_upload').uploadify({
            method:'post',
            multi:false,
            buttonClass:'selecButton',
            buttonText:'浏览',
            buttonImage:'<%=application.getContextPath()%>/njs/basd/uplodify/select.jpg',
            fileTypeDesc:'支持上传的文件格式：',
            swf:'<%=application.getContextPath()%>/njs/basd/uplodify/uploadify.swf',
            uploader:'<s:url action="uploadNews" namespace="/"/>',
            cancelImg:'<%=application.getContextPath()%>/njs/basd/uplodify/cancel.png',
            auto:true,
            fileSizeLimit:'30MB',
            requeueErrors:false,
            fileObjName: 'file',
            height:15,
            removeCompleted:false,
            width:30,
            onUploadComplete: function(file) {

            },
            onUploadSuccess: function(file,data,response) {
                var name = file.name;
                fileDivPathMap[file.id]=data;
                $.ajax({//填充告警数
                    url:'<s:url action="saveNewsFileDb" namespace="/"/>',
                    data:{map:{NAME:name,FILE_NAME:file.name,FILE_SIZE:file.size,FILE_PATH:data}},
                    type:'post',
                    success:function(data){
                        fileIds.push(data.GUID);
                        saveFileIds.push(data.GUID);
                        fileIdMap[file.id] = data.GUID;
                    }
                });

            },
            onUploadError: function(file) {
            },
            onSelect : function(file) {
            },
            onSelectError: function(file,errorCode,errorMsg){
                this.queueData.errorMsg = "请重新选择文件，且必须小于30M！";
            },
            onDialogClose : function(swfuploadifyQueue) {

            },
            onQueueComplete : function(stats) {

            }

        });

        if('${param.op}'=='view'&& $("[id='news.ID']").val()){
            $(":button:not(#btn-back)").each(function () {
                $(this).remove();
            });
            $(":input:not(.Wdate)").each(function () {
                $(this).attr("readonly","readonly");
            });
            $(".Wdate").each(function () {
                $(this).attr("disabled","disabled");
            });
            $(".token-input-delete-token-facebook").remove();
            $(".cancel").remove();
            $("#file_upload-button").remove();
            //查看模式 标记已读
            $(".form-title").html($(".form-title").html().replace("增加","查看"));
            var id = $("[id='news.ID']").val();
            $.ajax({
                url: 'readNews.do',
                type: "POST",
                data: {ids:[id]},
                cache: false,
                error:function(){
                    alert('标记失败');
                },
                success: function(msg){
                }
            })

        }else{

            if('${param.op}'=='view'&& $("[id='news.ID']").val()){
                $(".form-title").html($(".form-title").html().replace("增加","修改"));
            }

            $("[input-id='news.RCV_DEPT_ID']").click(function(){
                selectDept();
            })
            //添加人员事件
            $("[input-id='news.RCV_LOGIN_ID']").click(function(){
                selectUser();
            })
        }

        $("#btn-save").click(function(){
            $("#theForm").submit();
        })
        $("#theForm").validationEngine().ajaxForm({
            type:"post",
            beforeSubmit:function(data) {
                var TITLE =  $("[name='news.TITLE']").val();
                var CONTENT =  $("[name='news.CONTENT']").val();
                var RCV_LOGIN_ID = $("[name='news.RCV_LOGIN_ID']").val();
                var RCV_DEPT_ID = $("[name='news.RCV_DEPT_ID']").val();
                if(!TITLE && !CONTENT){
                    alert('标题或内容至少填写一项')
                    return false;
                }
                if(!RCV_LOGIN_ID && !RCV_DEPT_ID){
                    alert('请填写接收部门或接收人员')
                    return false;
                }
                if('${param.op}'=='edit'&& $("[id='news.ID']").val()){
                    data.push({name: "news.FILE_IDS", type: "hidden",value:fileIds});
                }else{
                    data.push({name: "news.FILE_IDS", type: "hidden",value:saveFileIds});
                }
                $("body").mask('正在保存,请稍后...');
            },
            success:function(data) {
                $("body").unmask();
                if(!data){
                    alert("该公告已生效,无法修改,请重新发布");
                }else{
                    window.close();
                }
            },
            error:function(xhr){
                $("body").unmask();
                alert(xhr);
            }
        });
    });

    function selectDept(){
        var selectDept = window.showModalDialog('<%=application.getContextPath()%>/loadDeptManage.do?type=select','选择部门','help:no;scroll:no;status:no;dialogHeight:600px;dialogWidth:900px');
        if(selectDept!=undefined){
            var id = [];
            var name = [];
            for (var one in selectDept){
                $("[id='news.RCV_DEPT_ID']").tokenInput("add", {id: selectDept[one].DEPT_ID, name:selectDept[one].DEPT_NAME});
            }
        }
    }

    function selectUser(){
        var selectUser = window.showModalDialog('/portal/loadUserManage.do?type=select','选择用户','help:no;scroll:no;status:no;dialogHeight:600px;dialogWidth:900px');
        if(selectUser!=undefined){
            var id = "";
            var name = "";
            for (var one in selectUser){
                $("[id='news.RCV_LOGIN_ID']").tokenInput("add", {id: selectUser[one].LOGIN_ID, name:selectUser[one].NAME});
            }
        }
    }

    $("[name='news.RCV_LOGIN_ID'],[name='news.RCV_DEPT_ID']").tokenInput("", {
        theme: "facebook",
        hintText: null,
        searchingText: null,
        noResultsText: null,
        preventDuplicates: true,
        minChars:50,
        onAdd: function () {
            var str = $(this).attr("tip");
            persistenceTokenInput($("[id='news."+str+"_ID']").tokenInput("get"),str);
        },
        onDelete: function () {
            var str = $(this).attr("tip");
            persistenceTokenInput($("[id='news."+str+"_ID']").tokenInput("get"),str);
        }
    }).blur();

    function persistenceTokenInput(items,str) {
        var names = [], ids = [];
        $.each(items, function () {
            ids.push(this.id);
            names.push(this.name);
        });
        $("[id='news."+str+"_ID']").val(ids.join(','));
        $("[id='news."+str+"_NAME']").val(names.join(','));
    }




</script>
</body>
</html>
